package com.google.code.jwebannotation;

import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

class BeanFunctions {

	private static Log log = LogFactory.getLog(BeanFunctions.class);

	public static Object populate(Class<?> clazz, HttpServletRequest request) throws InstantiationException, IllegalAccessException {
		Object obj = clazz.newInstance();

		Enumeration<?> names = request.getParameterNames();
		while (names.hasMoreElements()) {
			String name = (String) names.nextElement();
			if (log.isTraceEnabled()) log.trace("Setting arg '"+name+"'");
			try {
				BeanUtils.setProperty(obj, name, request.getParameter(name));
			} catch (InvocationTargetException e) {
				log.debug("Set property exception: ", e);
			}
		}
		return obj;
	}


}
